layui.define(function (exports) {

  const table = 'general'

  const storage = {
    /**
     * 设置
     * @param key
     * @param value
     */
    set: (key, value) => {
      if (!key || !value) {
        return
      }
      layui.data(table, {
        key: key,
        value: value,
      })
    },
    /**
     * 获取
     * @param key
     * @returns {*|null}
     */
    get: (key) => {
      let local = layui.data(table)
      return local[key] || null
    },
    /**
     * 删除
     * @param key
     */
    remove: (key) => {
      layui.data(table, {
        key: key,
        remove: true,
      })
    },

    /**
     * 添加到数组中
     * @param key
     * @param value
     */
    push: (key, value) => {

      let arr = storage.get(key)
      if (!arr) {
        arr = []
      }

      arr.push(value)

      layui.data(table, {
        key: key,
        value: arr,
      })
    },

    /**
     * 唯一值
     * @param key
     * @param value
     */
    pushUnique: (key, value) => {

      let arr = storage.get(key)
      if (!arr) {
        arr = []
      }

      if (arr.indexOf(value) === -1) {
        arr.push(value)
      }

      layui.data(table, {
        key: key,
        value: arr,
      })
    },

    /**
     *
     * @param key
     * @param value
     * @returns {boolean}
     */
    removeUnique: (key, value) => {
      let arr = storage.get(key)
      if (!arr) {
        return true
      }
      let index = arr.indexOf(value)
      if (index === -1) {
        return true
      }

      arr.splice(index, 1)

      storage.set(key, arr)
    },
  }

  exports('storage', storage)
})

